package com.pay.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pay.dto.MemberDto;
import com.pay.dto.ResponseEntity;
import com.pay.entity.TbBusinessOrder;
import com.pay.entity.TbMember;
import com.pay.mapper.TbMemberMapper;
import com.pay.service.TbBusinessOrderService;
import com.pay.mapper.TbBusinessOrderMapper;
import com.pay.utils.IdWorker;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 */
@Service
public class TbBusinessOrderServiceImpl extends ServiceImpl<TbBusinessOrderMapper, TbBusinessOrder>
    implements TbBusinessOrderService{
    @Resource
    TbMemberMapper tbMemberMapper;
    @Resource
    TbBusinessOrderMapper tbBusinessOrderMapper;
    @Resource
    IdWorker idWorker;

    @Override
    public ResponseEntity getLiveType() {
        List<TbMember> memberList = tbMemberMapper.selectList(null);
        return new ResponseEntity(memberList,200,"查询成功",true);
    }

    @Override
    public ResponseEntity selByTypeId(Integer id) {
        TbMember member = new TbMember();
        LocalDateTime dateTime = LocalDateTime.now();
        //根据穿回来的id查询数据
        TbMember tbMember = tbMemberMapper.selectById(id);
        //判断时间
        if(tbMember.getStartTime().isAfter(dateTime) || tbMember.getEndTime().isBefore(dateTime)){
            //不是折扣时间
            member.setId(tbMember.getId());
            member.setMemberDiscountPrice(new BigDecimal(0));
            member.setMemberName(tbMember.getMemberName());
            member.setMemberPrice(tbMember.getMemberPrice());
            return new ResponseEntity(member,200,"没有折扣",false);
        }
        return new ResponseEntity(tbMember,200,"折扣价格",true);
    }

    @Override
    public ResponseEntity saveOrder(MemberDto memberDto, String token) {
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        Integer uid = Integer.parseInt(claims.getId());
        TbBusinessOrder businessOrder = new TbBusinessOrder();
        //生成订单号
        long nextId = idWorker.nextId();
        businessOrder.setBusinessId(uid);
        businessOrder.setMemberName(memberDto.getMemberName());
        businessOrder.setOrderNum(nextId);
        businessOrder.setOrderPrice(memberDto.getMemberEndPrice());
        businessOrder.setPayState(0);
        businessOrder.setPayWay(1);
        businessOrder.setOrderTime(LocalDateTime.now());
        tbBusinessOrderMapper.insert(businessOrder);
        return new ResponseEntity(null,200,"订单生成成功",true);
    }
}




